﻿using System.Linq;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;

namespace CardPlay.DemoPack.Games.SirTommyVariants
{
    [Game("88F5D8E5-6ED7-4E50-8885-2AD964F1211D", LevelOfDifficulty.Medium, LevelOfSkill.ModerateSkill, DurationOfPlay.LessThan5Minutes, Family = FamilyName)]
    public class Colours : LadyBetty
    {
        protected override bool AllowedToBuildFoundation(MovePileAction action)
        {
            if (action.TargetPile.IsEmpty)
            {
                var foundationNumber = action.Location.GetOrdinal();
                var faceOfCardBeingAdded = action.PileBeingMoved.BottomCard.Face;
                var nonEmptyFoundation = this.NonEmptyPilesNamed(AnyFoundation).FirstOrDefault();

                return
                    (faceOfCardBeingAdded.Rank == Rank.Ace + foundationNumber) &&
                    (nonEmptyFoundation == null ||
                        ((foundationNumber + nonEmptyFoundation.GetOrdinal()) % 2 == 0
                            ? SuitSequence.SameColor(faceOfCardBeingAdded.Suit, nonEmptyFoundation.BottomCard.Suit)
                            : SuitSequence.DifferentColor(faceOfCardBeingAdded.Suit, nonEmptyFoundation.BottomCard.Suit)));
            }

            return action.IsBuildingFoundation(CardFaceSequence.SameColorAscendingRank);
        }

        protected override bool AllowedToBuildEmptyTableau(MovePileAction action)
        {
            return action.SourcePile.Location.Matches(Stock);
        }

        protected override bool AllowedToBuildNonEmptyTableau(MovePileAction action)
        {
            return !action.SourcePile.Location.Matches(AnyTableau);
        }
    }
}